<title>收入管理</title>

<div class="layui-fluid">
  <div class="layui-row layui-col-space15">
    <div class="layui-col-md12">
      <div class="layui-card">

        <div class="layui-card-header layuiadmin-card-header-auto"
             id="LAY_revenue_statistics_view"></div>

        <script type="text/html" id="LAY_revenue_statistics_tpl">
          <ul class="LAY_revenue_information">
            <li>
              <div>结算中</div>
              <p>{{d.settlement}}元</p>
            </li>
            <li>
              <div>待提现金额</div>
              <p>{{d.waitWithdrawal}}元</p>
            </li>
            <li>
              <div>提现中</div>
              <p>{{d.withdrawalIng}}元</p>
            </li>
            <li>
              <div>已提现</div>
              <p>{{d.withdrawalSuc}}元</p>
            </li>
            <li>
              <div>累计收入</div>
              <p>{{d.totalIncome}}元</p>
            </li>
          </ul>
          <div class="LAY_revenue_describe">
            <h3>结算细则：</h3>
            <p>1、服务费入账后可结算；短险的服务费入账日为保单生效日；长期险若有犹豫期，则“保单生效日+犹豫期”为服务费入账日;<br>
              2、每月初为您生成上月账单，即次月可提现上月服务费，也可以合并提现所有未提现月的服务费；<br>
              3、我们将在提现后10个工作日内（节假日顺延）完成服务费支付。<br>
              4、因银行转账时间的差异，结算收入将财务支付后在1-3日内到账，若结算申请状态显示“已结算”，即为已通过银行进行付款转账。<br>
              5、开具发票：会员请根据申请金额开具服务费发票。<br>
              6、发票填写：请根据开票信息，确保 发票抬头、发票项目、发票金额以及发票印章准确无误。<br>
              7、发票开具并寄送后，点击“申请结算”。我司将在收到发票后进行结算。</p>
          </div>
          <div class="LAY_revenue_btn_group">
            <button class="layui-btn LAY_revenue_btn_withdraw">我要提现</button>
            <button class="layui-btn layui-btn-primary LAY_revenue_btn_total_detail" lay-href="/revenue/detail">查看总明细</button>
          </div>
        </script>

        <div class="layui-card-body">
          <div class="LAY_wisdom_title">
            <h3 class="LAY_wisdom_title_line">提现历史</h3>
          </div>

          <table id="LAY_revenue_histroy" lay-filter="LAY_revenue_histroy"></table>

          <script type="text/html" id="LAY_revenue_histroy_toolbar">
            <a class="layui-btn layui-btn-xs LAY_revenue_list_btn_total" lay-event="detail">查看明细</a>
            {{# if (d.status == 3) { }}
            <a class="layui-btn layui-btn-xs LAY_revenue_list_btn_reason" lay-event="reason">查看失败原因</a>
            {{# } }}
          </script>
        </div>
      </div>
    </div>
  </div>
</div>

<script>
  layui.use(['admin', 'laytpl', 'table', 'layer', 'getmenus'], function () {
    var $ = layui.$,
            admin = layui.admin,
            setter = layui.setter,
            layer = layui.layer,
            table = layui.table,
            laytpl = layui.laytpl,
            element = layui.element,
            getmenus = layui.getmenus;

    admin.req({
      url: setter.url + '/finance/summary',
      type: 'GET',
      done: function (res) {
        var data = res.data || [];
        renderData(data, function () {
          initPermission();
          initEvents();
        });
      }
    });

    function renderData(data, callback) {
      var getTpl = $('#LAY_revenue_statistics_tpl').html();

      if (!getTpl) {
        return;
      }
      laytpl(getTpl).render(data, function(html){
        $('#LAY_revenue_statistics_view').html(html);

        if (typeof(callback) === 'function') {
          callback();
        }
      });
    }

    function initEvents() {
      $('.LAY_revenue_btn_withdraw').off('click').on('click', function () {
        admin.req({
          url: setter.url + '/finance/checkAccount',
          type: 'GET',
          done: function (res) {
            var data = res.data || [];

            if (data) {
              location.hash = '/revenue/withdraw';
              return;
            }

            layer.confirm('请您先完善自己的结算账户信息，我们才能提现', {
              title: '移出确认',
              btn: ['立即前往', '取消']
            }, function (index, layero) {
              // 点击立即前往跳转至自己的机构详情页面
              layer.close(index);
            });
          }
        });
      });
    }

    table.render({
      elem: '#LAY_revenue_histroy'
      ,url: setter.url + '/finance/queryWithdrawalList' //模拟接口
      ,page: true
      ,parseData: function(res){
        return {
          "code": res.code,
          "msg": res.msg,
          "count": res.data.recordCounts,
          "data": res.data.datas
        };
      }
      ,response: {
        statusCode: 200 //规定成功的状态码，默认：0
      }
      ,request: {
        limitName: 'pageSize'
      }
      ,cols: [[
        { field: 'applyTime', align: 'center', title: '提现申请时间' }
        ,{ field: 'settlementCycle', align: 'center', title: '结算周期' }
        ,{ field: 'amount', align: 'center', title: '提现金额(元)' }
        ,{ field: 'statusDesc', align: 'center', title: '提现状态' }
        ,{ title: '操作', width: 300, align: 'center', fixed: 'right',  toolbar: '#LAY_revenue_histroy_toolbar' }
      ]]
      ,done: function () {
        initTablePermission();
      }
    });

    table.on('tool(LAY_revenue_histroy)', function(obj){
      var data = obj.data;

      switch (obj.event) {
        case 'detail':
          location.hash = '/revenue/detail/settlementCycle=' + data.settlementCycle
          break;
        case 'reason':
          var content =
                  '<ul>' +
                  '<li>结算周期：' + data.settlementCycle + '</li>' +
                  '<li>失败原因：' + data.faiMsg + '</li>' +
                  '</ul>';

          layer.open({
            title: '查看失败原因',
            content: content,
            area: ['600px'],
            btn: ['我知道了']
          });
          break;

        default:
          break;
      }
    });

    function initPermission() {
      console.log(getmenus, getmenus.toWithdrawal, getmenus.allDetail)
      // 我要提现
      var withdrawBtn = $('.LAY_revenue_btn_withdraw');

      if (!getmenus.toWithdrawal) {
        withdrawBtn.remove();
      }

      // 查看总明细
      var totalDetailBtn = $('.LAY_revenue_btn_total_detail');

      if (!getmenus.allDetail) {
        totalDetailBtn.remove();
      }
    }

    function initTablePermission() {
      // 查看明细
      var detailBtn = $('.LAY_revenue_list_btn_total');

      if (!getmenus.detail) {
        detailBtn.remove();
      }
      // 查看失败原因
      var reasonBtn = $('.LAY_revenue_list_btn_reason');

      if (!getmenus.toFaile) {
        reasonBtn.remove();
      }
    }

  });
</script>